package com.itheima.utils;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.itheima.eneity.SMSResult;

public class SMSUtils {

    /*短信模版*/
    public static final String VALIDATE_CODE = "SMS_174028380";//发送短信验证码,已申请通过
    public static final String ORDER_NOTICE = "SMS_159771588";//体检预约成功通知

    /**
     * 发送短信
     * @param phoneNumbers
     * @param param
     * @throws ClientException
     */
    public static SMSResult sendShortMessage(String templateCode,String phoneNumbers,String param) throws ClientException{
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FfrnjekteGmhJwfd753", "oBdxvNMVqTpO09FfKN8HCkq8AJbeKM");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("SignName", "体检预约");
        request.putQueryParameter("TemplateCode", templateCode);


        request.putQueryParameter("TemplateParam", "{\"code\":\""+param+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            /*将json字符串转换为对象*/
            return JSON.parseObject(response.getData(), SMSResult.class);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

}